// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
let panelBase = require("panelBase")
let viewConfig = {
    btnBack:{
        node:null,
        name:"btn_back",
        eventType:"click",
        cb:"onBtnCloseClick"
    },
    item:{
        node:null,
        name:"item",
        active:false,
    },
    item_my:{
        node:null,
        name:"item_mySelf",
    },
    sc:{
        node:null,
        name:"shopSc",
    }
}
cc.Class({
    extends: panelBase,

    properties: {
        rankList:[],
        mySc:null,
    },
    init(){
        this._super(viewConfig);
        this.isInited = true;
        this.rankList = this.getRankList();
        this.node.addComponent("myScrollView");
        this.mySc = this.node.getComponent("myScrollView");
        cc.tg.eventCenter.addEvent("onRefreshGGKRank",(data)=>{
            this.refreshRank(data);
        })
        this.mySc.init(viewConfig["sc"].node);
    },

    onShow(){
        this._super();
        cc.tg.gameLogic.toRefreshGGKRank();
    },
    refreshRank(data){
        if(!data)
            return ;
        let testList = [];
        for(let i = 0 ; i < data.length;++i)
        {
            let d = {
                rank:i,
                nickName:data[i].accName,
                coin:data[i].coin,
                reward:"没有奖励"
            }
            testList.push(d);
        }
        this.mySc.showData(viewConfig["item"].node,testList,this.initItem.bind(this),data.length);
        let myData = {
            rank:99,
            nickName:cc.tg.cakeRoom.accName,
            coin:cc.tg.cakeRoom.accGold,
            reward:"没有奖励"
        }
        this.initItem(viewConfig["item_my"].node,myData)
    },

    initItem(node,data){
        if(!node || !data)
            return ;
        let levelBg = node.getChildByName("rank_bg");
        let head = node.getChildByName("head");
        this.updateInfo(head,this.getHeadByIndex(data.rank),cc.Sprite)
        if(data.rank <= 3)
        {
            this.updateInfo(levelBg,this.rankList[data.rank-1],cc.Sprite);
        }
        else
        {
            this.updateInfo(levelBg,null,cc.Sprite);
        }
        this.updateInfo(node.getChildByName("rankLabel"),data.rank,cc.Label);
        this.updateInfo(node.getChildByName("nickName"),data.nickName,cc.Label);
       // this.updateInfo(node.getChildByName("coinLabel"),data.coin,cc.Label);
        this.updateInfo(node.getChildByName("levelLabel"),data.level,cc.Label);
        this.updateInfo(node.getChildByName("reward"),data.reward,cc.Label);
    },
    onBtnCloseClick(){
       // cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"ggk",active:true});
        this.onHide();
    },
    onDestroy(){
        for(let key in viewConfig)
        {
            viewConfig[key].node = null;
        }
    },
    
});
